package models.midas.form;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import services.midas.assembler.Assembler;

public class CommandClassForm extends BaseFormBean{
	
	private long classLetterId;
	private String classLetter;	
	private String acceptedDate;
	private String enMenuText;
	private String elementsIds;
	private Map<Long,String> networkElements;
	private String application;
	private String buildsIds;
	private Map<Long,String> builds;
	private String status;
	
	private String createdAt;
	
	public CommandClassForm(JSONObject data){
		super(data);
	}
	
	public CommandClassForm(){
		init();
	}
	
	@Override
	public JSONObject getJsonData() throws JSONException{
		JSONObject data = new JSONObject();
		data.put("id", this.getId());
		data.put("classLetterId", this.getClassLetterId());
		data.put("state", this.getState());
		data.put("grammar", this.isGrammar());
		data.put("acceptedDate", this.getAcceptedDate());
		data.put("menuText", this.getEnMenuText());
		data.put("networkElements", this.getElementsIds());
		data.put("application", this.getApplication());
		data.put("builds", this.getBuildsIds());
		data.put("status", this.getStatus());
		data.put("author", this.buildAuthorJSON());
		data.put("createdAt", this.getCreatedAt());
		data.put("version", this.getVersion());
		data.put("comments", this.getComments());
		return data;
	}
	
	@Override
	protected void retriveDataFromJson(JSONObject data) throws JSONException{
		init();
		this.setId(data.getLong("id"));
		this.retriveClassLetter(data.getJSONObject("classLetter"));
		this.setState(data.getString("state"));
		this.setGrammar(data.getBoolean("grammar"));
		this.setAcceptedDate(data.getString("acceptedDate"));
		this.setEnMenuText(data.getString("menuText"));
		this.setNetworkElements(Assembler.jsonArray2Map(data.getJSONArray("networkElements")));
		this.setApplication(data.getString("application"));
		this.setBuilds(retriveBuildsArray(data.getJSONArray("builds")));
		this.setStatus(data.getString("status"));
		this.retriveAuthor(data.getJSONObject("author"));
		this.setCreatedAt(data.getString("createdAt"));
		this.setVersion(data.getString("version"));
		//this.setComments(data.getString("comments"));
		this.setRevisionHistory(data.getString("revisionHistory"));
		//this.setEditHistory(data.getString("editHistory"));
		
	}
	
	private void init(){
		networkElements = new HashMap<Long,String>();
		builds = new HashMap<Long,String>();
	}
	
	private void retriveClassLetter(JSONObject classLetter) throws JSONException{
		this.setClassLetterId(classLetter.getLong("id"));
		this.setClassLetter(classLetter.getString("letter"));
	}
	
	private void retriveAuthor(JSONObject author) throws JSONException{
		this.setAuthorId(author.getInt("id"));
		this.setAuthor(author.getString("name"));
		this.setEmail(author.getString("email"));
	}
	
	private JSONObject buildAuthorJSON() throws JSONException{
		JSONObject object = new JSONObject();
		object.put("id", this.getAuthorId());
		object.put("name", this.getAuthor());
		object.put("email", this.getEmail());
		return object;
	}

	public String getClassLetter() {
		return classLetter;
	}

	public long getClassLetterId() {
		return classLetterId;
	}

	public void setClassLetterId(long classLetterId) {
		this.classLetterId = classLetterId;
	}

	public void setClassLetter(String classLetter) {
		this.classLetter = classLetter;
	}

	public String getAcceptedDate() {
		return acceptedDate;
	}

	public void setAcceptedDate(String acceptedDate) {
		this.acceptedDate = acceptedDate;
	}

	public String getEnMenuText() {
		return enMenuText;
	}

	public void setEnMenuText(String enMenuText) {
		this.enMenuText = enMenuText;
	}

	public String getElementsIds() {
		return elementsIds;
	}

	public void setElementsIds(String elementsIds) {
		this.elementsIds = elementsIds;
	}

	public Map<Long, String> getNetworkElements() {
		return networkElements;
	}

	public void setNetworkElements(Map<Long, String> networkElements) {
		this.networkElements = networkElements;
		this.setElementsIds(Assembler.collection2String(this.networkElements.keySet(),","));
	}

	public String getApplication() {
		return application;
	}

	public void setApplication(String application) {
		this.application = application;
	}

	public String getBuildsIds() {
		return buildsIds;
	}

	public void setBuildsIds(String buildsIds) {
		this.buildsIds = buildsIds;
	}

	public String getBuilds() {
		return Assembler.collection2String(this.builds.values(),";");
	}

	public void setBuilds(Map<Long, String> builds) {
		this.builds = builds;
		this.setBuildsIds(Assembler.collection2String(this.builds.keySet(),","));
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(String createdAt) {
		this.createdAt = createdAt;
	}
}
